<template>
  <div id="questionView">
    <a-form :model="searchParams" layout="inline">
      <a-form-item field="title" label="名称">
        <a-input
          v-model="searchParams.title"
          placeholder="请输入题目"
          style="min-width: 200px"
        />
      </a-form-item>
      <a-form-item field="tags" label="标签">
        <a-input-tag
          v-model="searchParams.tags"
          placeholder="请输入标签"
          style="min-width: 200px"
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" @click="doSearch">查询</a-button>
      </a-form-item>
    </a-form>
    <a-table
      :columns="columns"
      :data="dataList"
      :pagination="{
        showTotal: true,
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total: total,
      }"
      @page-change="onPageChange"
    >
      <template #tags="{ record }">
        <a-space>
          <a-tag v-for="(tag, index) of record.tags" :key="index" color="green"
            >{{ tag }}
          </a-tag>
        </a-space>
      </template>
      <template #acceptedRate="{ record }">
        {{
          `(${record.acceptNum} / ${record.submitNum})  ${
            record.acceptNum / (record.submitNum === 0 ? 1 : record.submitNum)
          }% `
        }}
      </template>
      <template #createTime="{ record }">
        {{ moment(record.createTime).format("YYYY-MM-DD ") }}
      </template>

      <template #optional="{ record }">
        <a-space>
          <a-button @click="toQuestion(record.id)" type="primary"
            >去做题
          </a-button>
        </a-space>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import {
  DeleteRequest,
  QuestionControllerService,
  QuestionQueryRequest,
  QuestionView,
} from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import moment from "moment";

const columns = [
  {
    title: "ID",
    dataIndex: "id",
  },
  {
    title: "题目",
    dataIndex: "title",
  },
  {
    title: "标签",
    slotName: "tags",
  },
  {
    title: "通过率(通过数/提交数)",
    slotName: "acceptedRate",
  },

  {
    title: "创建时间",
    slotName: "createTime",
  },

  {
    slotName: "optional",
  },
];
const dataList = ref([]);
const total = ref(0);
const searchParams = ref({
  pageSize: 10,
  current: 1,
  tags: [],
  title: "",
} as QuestionQueryRequest);

const doSearch = () => {
  //如果用户在第二页，搜索时current还是2 可能出现数据存在但是搜索不到的情况

  searchParams.value = {
    ...searchParams.value,
    current: 1,
  };
  // loadData();
};

const loadData = async () => {
  const res = await QuestionControllerService.listQuestionVoByPageUsingPost(
    searchParams.value
  );
  if (res.code === 200) {
    dataList.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error("查询失败:", res.message);
  }
};

// 翻页功能
const onPageChange = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: page,
  };
};
watchEffect(() => {
  loadData();
});
//跳转做题页面
const router = useRouter();

const toQuestion = async (id) => {
  router.push({
    path: `/question/do/${id}`,
  });
};
</script>

<style scoped>
#manageQuestionView {
  max-width: 1280px;
  marrgin: 0 auto;
}
</style>
